package com.berchen.数据结构.二叉树;


public class BinaryTree {
    public TreeNode root;


    /**
     * 前序查找
     * @param node
     * @return
     */
    public TreeNode preSearch(TreeNode node,int id){
        if(node.getId()==id){
            return node;
        }
        TreeNode treeNode = null;
        if(node.left!=null){
            treeNode=preSearch(node.left,id);
        }
        if(treeNode!=null){
            return treeNode;
        }
        if(node.right!=null){
            treeNode=preSearch(node.right,id);
        }
        return treeNode;
    }

    /**
     * 中序查找
     * @param node
     * @param id
     * @return
     */
    public TreeNode innSearch(TreeNode node,int id){

        TreeNode treeNode = null;
        if(node.left!=null){
            treeNode= innSearch(node.left,id);
        }
        if(treeNode!=null){
            return treeNode;
        }
        if(node.getId()==id){
            return node;
        }
        if(node.right!=null){
            treeNode=innSearch(node.right,id);
        }
        return treeNode;
    }

    /**
     * 后序
     * @param node
     * @param id
     * @return
     */
    public TreeNode sufSearch(TreeNode node,int id){
        TreeNode treeNode = null;
        if(node.left!=null){
            treeNode= sufSearch(node.left,id);
        }
        if(treeNode!=null){
            return treeNode;
        }
        if(node.right!=null){
            treeNode=sufSearch(node.right,id);
        }
        if(treeNode!=null){
            return treeNode;
        }
        if(node.getId()==id){
            return node;
        }
        return null;
    }

    /**
     * 前序遍历
     * @param root
     */
    public void preOrder(TreeNode root){
        System.out.println(root.toString());
        if(root.left!=null){
            preOrder(root.left);
        }
        if(root.right!=null){
            preOrder(root.right);
        }
    }

    /**
     * 中序遍历
     * @param root
     */
    public void innOrder(TreeNode root){
        if(root.left!=null){
            preOrder(root.left);
        }
        System.out.println(root.toString());
        if(root.right!=null){
            preOrder(root.right);
        }
    }
    /**
     * 后续遍历
     */
    public void sufOrder(TreeNode root){

        if(root.left!=null){
            preOrder(root.left);
        }
        if(root.right!=null){
            preOrder(root.right);
        }
        System.out.println(root.toString());
    }
}
